import DataBus from '../databus'

export default class AIUndoTest {
  constructor() {
    this.databus = new DataBus()
    this.testResults = []
  }

  runAllTests() {
    console.log('开始运行AI模式悔棋功能测试...')
    
    this.testAIModeUndoRequiresTwoMoves()
    this.testAIModeUndoRemovesBothMoves()
    this.testAIModeUndoReturnsToPlayer()
    this.testAIModeUndoCountDecreases()
    this.testAIModeUndoHistoryTracking()
    this.testAIModeUndoButtonDisabled()
    this.testAIModeUndoButtonEnabled()
    
    this.printResults()
  }

  testAIModeUndoRequiresTwoMoves() {
    console.log('测试：AI模式悔棋需要至少2步历史')
    
    // 设置AI模式
    this.databus.setAIMode(true, 'easy', 2)
    this.databus.currentPlayer = 1
    
    // 只有1步历史时，不能悔棋
    this.databus.recordMove(7, 7, 1)
    
    const canUndo = this.databus.moveHistory.length >= 2
    const result = !canUndo
    
    this.testResults.push({
      name: 'AI模式悔棋需要至少2步历史',
      passed: result,
      message: result ? '测试通过' : '测试失败：只有1步历史时应该不能悔棋'
    })
  }

  testAIModeUndoRemovesBothMoves() {
    console.log('测试：AI模式悔棋移除两步')
    
    // 设置AI模式
    this.databus.setAIMode(true, 'easy', 2)
    this.databus.currentPlayer = 1
    
    // 模拟两步历史：玩家落子 + AI落子
    this.databus.board[7][7] = 1
    this.databus.recordMove(7, 7, 1)
    this.databus.board[8][8] = 2
    this.databus.recordMove(8, 8, 2)
    
    const initialHistoryLength = this.databus.moveHistory.length
    const undoneMoves = this.databus.undoAIMoves()
    
    const result = undoneMoves && 
                   this.databus.board[7][7] === 0 && 
                   this.databus.board[8][8] === 0 &&
                   this.databus.moveHistory.length === initialHistoryLength - 2
    
    this.testResults.push({
      name: 'AI模式悔棋移除两步',
      passed: result,
      message: result ? '测试通过' : '测试失败：悔棋应该移除玩家和AI的落子'
    })
  }

  testAIModeUndoReturnsToPlayer() {
    console.log('测试：AI模式悔棋后回到玩家回合')
    
    // 设置AI模式
    this.databus.setAIMode(true, 'easy', 2)
    this.databus.currentPlayer = 2 // AI回合
    
    // 模拟两步历史
    this.databus.board[7][7] = 1
    this.databus.recordMove(7, 7, 1)
    this.databus.board[8][8] = 2
    this.databus.recordMove(8, 8, 2)
    
    // 执行悔棋
    this.databus.undoAIMoves()
    this.databus.currentPlayer = 1 // 回到玩家回合
    
    const result = this.databus.currentPlayer === 1
    
    this.testResults.push({
      name: 'AI模式悔棋后回到玩家回合',
      passed: result,
      message: result ? '测试通过' : '测试失败：悔棋后应该回到玩家回合'
    })
  }

  testAIModeUndoCountDecreases() {
    console.log('测试：AI模式悔棋次数减少')
    
    const player = 1
    const initialCount = this.databus.getUndoCount(player)
    
    // 使用一次悔棋
    this.databus.useUndo(player)
    const afterCount = this.databus.getUndoCount(player)
    
    const result = afterCount === initialCount - 1
    
    this.testResults.push({
      name: 'AI模式悔棋次数减少',
      passed: result,
      message: result ? '测试通过' : '测试失败：悔棋次数应该减少1'
    })
  }

  testAIModeUndoHistoryTracking() {
    console.log('测试：AI模式落子历史记录')
    
    // 设置AI模式
    this.databus.setAIMode(true, 'easy', 2)
    
    // 模拟玩家落子
    this.databus.board[7][7] = 1
    this.databus.recordMove(7, 7, 1)
    
    // 模拟AI落子
    this.databus.board[8][8] = 2
    this.databus.recordMove(8, 8, 2)
    
    const result = this.databus.moveHistory.length === 2 &&
                   this.databus.moveHistory[0].player === 1 &&
                   this.databus.moveHistory[1].player === 2
    
    this.testResults.push({
      name: 'AI模式落子历史记录',
      passed: result,
      message: result ? '测试通过' : '测试失败：应该正确记录玩家和AI的落子历史'
    })
  }

  testAIModeUndoButtonDisabled() {
    console.log('测试：AI模式悔棋按钮禁用条件')
    
    // 设置AI模式
    this.databus.setAIMode(true, 'easy', 2)
    
    // 测试AI回合时按钮应该禁用
    this.databus.currentPlayer = 2
    const aiTurnDisabled = this.databus.currentPlayer === this.databus.aiPlayer
    
    // 测试历史不足时按钮应该禁用
    this.databus.currentPlayer = 1
    const historyInsufficient = this.databus.moveHistory.length < 2
    
    const result = aiTurnDisabled && historyInsufficient
    
    this.testResults.push({
      name: 'AI模式悔棋按钮禁用条件',
      passed: result,
      message: result ? '测试通过' : '测试失败：AI回合和历史不足时按钮应该禁用'
    })
  }

  testAIModeUndoButtonEnabled() {
    console.log('测试：AI模式悔棋按钮启用条件')
    
    // 设置AI模式
    this.databus.setAIMode(true, 'easy', 2)
    this.databus.currentPlayer = 1
    
    // 模拟足够的落子历史
    this.databus.board[7][7] = 1
    this.databus.recordMove(7, 7, 1)
    this.databus.board[8][8] = 2
    this.databus.recordMove(8, 8, 2)
    
    // 测试玩家回合且有足够历史时按钮应该启用
    const isPlayerTurn = this.databus.currentPlayer !== this.databus.aiPlayer
    const hasEnoughHistory = this.databus.moveHistory.length >= 2
    const hasUndoCount = this.databus.canUndo(this.databus.currentPlayer)
    
    const result = isPlayerTurn && hasEnoughHistory && hasUndoCount
    
    this.testResults.push({
      name: 'AI模式悔棋按钮启用条件',
      passed: result,
      message: result ? '测试通过' : '测试失败：玩家回合且有足够历史时按钮应该启用'
    })
  }

  printResults() {
    console.log('\n=== AI模式悔棋功能测试结果 ===')
    let passedCount = 0
    let totalCount = this.testResults.length
    
    this.testResults.forEach((test, index) => {
      const status = test.passed ? '✅ 通过' : '❌ 失败'
      console.log(`${index + 1}. ${test.name}: ${status}`)
      if (!test.passed) {
        console.log(`   原因: ${test.message}`)
      } else {
        passedCount++
      }
    })
    
    console.log(`\n总计: ${passedCount}/${totalCount} 测试通过`)
    
    if (passedCount === totalCount) {
      console.log('🎉 所有测试通过！AI模式悔棋功能正常。')
    } else {
      console.log('⚠️  部分测试失败，需要检查代码。')
    }
  }
} 